package com.litbw.user;
import javax.swing.*;


import java.awt.event.*;
import java.awt.*;
import static javax.swing.JOptionPane.*;
/**
 * @author Derrick Mink
 */
public class ImageViewTester implements ActionListener {
	private JFrame frame;
	private String fileName;
	private JMenuItem menuItem[] = new JMenuItem[2];
	private JLabel area = new JLabel();
	private JScrollPane JSp = new JScrollPane(area,
			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	private JFileChooser dialog = new JFileChooser();

	private ImageViewer viewer;

	public ImageViewTester() {
		viewer = new ImageViewer(500, 500);
		frame = new JFrame();

		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("File");
		frame.add(menuBar, BorderLayout.NORTH);
		frame.add(JSp);
		frame.add(viewer, BorderLayout.CENTER);

		menuBar.add(menu);
		menuItem[0] = new JMenuItem("Open...");
		menuItem[1] = new JMenuItem("Exit");
		menuItem[0].addActionListener(this);
		menuItem[1].addActionListener(this);
		menuItem[1].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
				Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		menuItem[0].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
				Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
		menu.add(menuItem[0]);
		menu.add(menuItem[1]);
		frame.setJMenuBar(menuBar);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setTitle(fileName);
		frame.setResizable(true);
		frame.pack();
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == menuItem[0]) {
			if (dialog.showOpenDialog(null) == (JFileChooser.APPROVE_OPTION)) {
				boolean hasFile = viewer.readInFile(dialog.getSelectedFile()
						.getAbsolutePath(), 1);
				System.out.println(hasFile);
				if (!hasFile) {
					showMessageDialog(frame, "Does not compute !",
							"No Image file", INFORMATION_MESSAGE);
				} else {
					viewer.setImage();
					viewer.repaint();
					frame.repaint();
					frame.pack();
				}
			}
		} else if (e.getSource() == menuItem[1]) {
			System.exit(0);
		}
	}

}